package webpage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class DeclineRequest {
	public String declineRequest(String loginEmail,String emailaddress){
	String resultDecline = "Couldn't decline friend request";	
	String path = "C:/SVN/WebContent/Members/"+loginEmail+"/pendingrequests.txt";
	File file = new File(path);
	try {
		File temp = File.createTempFile("file", ".txt", file.getParentFile());
		String charset = "UTF-8";
		String delete = emailaddress;
		BufferedReader reader = new BufferedReader(new FileReader(file));
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
		for (String line; (line = reader.readLine()) != null;) {
			line = line.replace(delete, "");
			writer.println(line);
			System.out.println(line);
		}
		reader.close();
		writer.close();
		file.delete();
		temp.renameTo(file);
		temp.delete();
		resultDecline = "Friend Request successfully declined.";
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	System.out.println(resultDecline);
	return resultDecline;

}
}